package chapter10.eg3;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.TooLongFrameException;
import java.util.List;

public class SafeByteToMessageDecoder extends ByteToMessageDecoder {
  private static final int MAX_FRAME_SIZE = 1024;

  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    int readableBytes = in.readableBytes();
    if (readableBytes > MAX_FRAME_SIZE) {
      in.skipBytes(readableBytes);
      throw new TooLongFrameException("Frame to big!");
    }

    // do something
  }
}
